/*
 *  Copyright (C) 2010 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_addNetwork.java
 *
 * Created on 24/12/2010, 13:44:57
 */

package kuasar.plugin.vmcreator.gui.tooltasks.AddHost;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_AddNetwork extends kuasar.plugin.classMod.AbstractPanel {
    private pn_TB_AddHost toolbar;
    private DefaultListModel model = new DefaultListModel();
    private int maxdevices=10;
    /** Creates new form pn_addNetwork */
    public pn_AddNetwork(pn_TB_AddHost toolbar) {
        this.toolbar= toolbar;
        initComponents();
        lst_NetDevices.setCellRenderer(new Lst_Network_CellRender());
        lst_NetDevices.setModel(model);
        spn_NetDevices.setVisible(false);
        spn_NetDevices.getViewport().setOpaque(false);
        this.txt_Name.setText("");
        checkMaxDevices();
        Load();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pn_Container = new javax.swing.JPanel();
        txt_Name = new javax.swing.JTextField();
        spn_NetDevices = new javax.swing.JScrollPane();
        lst_NetDevices = new javax.swing.JList();
        cbb_Type = new javax.swing.JComboBox();
        btn_Add = new javax.swing.JButton();
        lbl_type = new javax.swing.JLabel();
        lbl_Name = new javax.swing.JLabel();
        lbl_info = new javax.swing.JLabel();
        btn_Cancel = new javax.swing.JButton();
        btn_Next = new javax.swing.JButton();
        btn_Previous = new javax.swing.JButton();

        setOpaque(false);

        pn_Container.setOpaque(false);

        txt_Name.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txt_NameFocusGained(evt);
            }
        });
        txt_Name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_NameKeyReleased(evt);
            }
        });

        spn_NetDevices.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_NetDevices.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_NetDevices.setOpaque(false);

        lst_NetDevices.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        lst_NetDevices.setOpaque(false);
        lst_NetDevices.setValueIsAdjusting(true);
        lst_NetDevices.setVisibleRowCount(5);
        lst_NetDevices.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                lst_NetDevicesKeyReleased(evt);
            }
        });
        spn_NetDevices.setViewportView(lst_NetDevices);

        cbb_Type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NAT", "Bridge", "Internal Network", "Host" }));
        cbb_Type.setSelectedIndex(1);
        cbb_Type.setRequestFocusEnabled(false);

        btn_Add.setBackground(new java.awt.Color(0, 0, 0));
        btn_Add.setForeground(new java.awt.Color(204, 204, 204));
        btn_Add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/vmcreator/icons/attach.png"))); // NOI18N
        btn_Add.setText("Add");
        btn_Add.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Add.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        btn_Add.setRequestFocusEnabled(false);
        btn_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AddActionPerformed(evt);
            }
        });

        lbl_type.setForeground(new java.awt.Color(204, 204, 204));
        lbl_type.setText("Type:");
        lbl_type.setRequestFocusEnabled(false);

        lbl_Name.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Name.setText("Name:");
        lbl_Name.setRequestFocusEnabled(false);

        lbl_info.setForeground(new java.awt.Color(204, 204, 204));
        lbl_info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout pn_ContainerLayout = new javax.swing.GroupLayout(pn_Container);
        pn_Container.setLayout(pn_ContainerLayout);
        pn_ContainerLayout.setHorizontalGroup(
            pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_ContainerLayout.createSequentialGroup()
                .addGroup(pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pn_ContainerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbl_info, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pn_ContainerLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_Name)
                            .addComponent(lbl_type))
                        .addGap(12, 12, 12)
                        .addGroup(pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbb_Type, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_Add, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(44, 44, 44)
                .addComponent(spn_NetDevices, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(66, Short.MAX_VALUE))
        );
        pn_ContainerLayout.setVerticalGroup(
            pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_ContainerLayout.createSequentialGroup()
                .addGroup(pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pn_ContainerLayout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(pn_ContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(pn_ContainerLayout.createSequentialGroup()
                                .addComponent(lbl_Name)
                                .addGap(22, 22, 22)
                                .addComponent(lbl_type, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(pn_ContainerLayout.createSequentialGroup()
                                .addComponent(txt_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbb_Type, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(22, 22, 22)
                        .addComponent(btn_Add)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_info))
                    .addGroup(pn_ContainerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(spn_NetDevices, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)))
                .addContainerGap())
        );

        btn_Cancel.setBackground(new java.awt.Color(0, 0, 0));
        btn_Cancel.setForeground(new java.awt.Color(204, 204, 204));
        btn_Cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/vmcreator/icons/dialog-cancel.png"))); // NOI18N
        btn_Cancel.setText("Cancel");
        btn_Cancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CancelActionPerformed(evt);
            }
        });

        btn_Next.setBackground(new java.awt.Color(0, 0, 0));
        btn_Next.setForeground(new java.awt.Color(204, 204, 204));
        btn_Next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/vmcreator/icons/dialog-next.png"))); // NOI18N
        btn_Next.setText("Next");
        btn_Next.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_NextActionPerformed(evt);
            }
        });

        btn_Previous.setBackground(new java.awt.Color(0, 0, 0));
        btn_Previous.setForeground(new java.awt.Color(204, 204, 204));
        btn_Previous.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/vmcreator/icons/dialog-previous.png"))); // NOI18N
        btn_Previous.setText("Previous");
        btn_Previous.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Previous.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_PreviousActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(238, Short.MAX_VALUE)
                        .addComponent(btn_Previous)
                        .addGap(6, 6, 6)
                        .addComponent(btn_Next)
                        .addGap(6, 6, 6)
                        .addComponent(btn_Cancel))
                    .addComponent(pn_Container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pn_Container, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_Previous)
                    .addComponent(btn_Next)
                    .addComponent(btn_Cancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AddActionPerformed
        addDevice();
    }//GEN-LAST:event_btn_AddActionPerformed

    private void txt_NameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_NameKeyReleased
       if(txt_Name.getBackground().equals(Color.pink))
           txt_Name.setBackground(Color.white);
        if(evt.getKeyCode()== KeyEvent.VK_ENTER)
           addDevice();
    }//GEN-LAST:event_txt_NameKeyReleased

    private void txt_NameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_NameFocusGained
        if(txt_Name.getBackground().equals(Color.pink))
            txt_Name.setBackground(Color.white);
    }//GEN-LAST:event_txt_NameFocusGained

    private void lst_NetDevicesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lst_NetDevicesKeyReleased
        
        if(evt.getKeyCode() == KeyEvent.VK_DELETE){
            lbl_info.setText("");
            if(!btn_Add.isEnabled())btn_Add.setEnabled(true);
            int indices[] =lst_NetDevices.getSelectedIndices();
            for(int i=indices.length -1 ; i>=0;i--){
              model.remove(indices[i]);
            }
            if(model.getSize()==0)
                spn_NetDevices.setVisible(false);
        }
    }//GEN-LAST:event_lst_NetDevicesKeyReleased

    private void btn_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CancelActionPerformed
        toolbar.exit();
}//GEN-LAST:event_btn_CancelActionPerformed

    private void btn_NextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_NextActionPerformed
        save();
        toolbar.next();
}//GEN-LAST:event_btn_NextActionPerformed

    private void btn_PreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_PreviousActionPerformed
        toolbar.previous();
}//GEN-LAST:event_btn_PreviousActionPerformed
    private void checkMaxDevices(){
        if(toolbar.data.get(Namespace.keyMaps.HYPERVISOR).equals(Namespace.HiperVisors.VIRTUALBOX)){
            maxdevices=4;
        }
    }
    private void addDevice(){
        if(txt_Name.getText().trim().isEmpty()){
            txt_Name.setBackground(Color.pink);
            return;
        }
        addDevice(txt_Name.getText().trim(), cbb_Type.getSelectedIndex());
    }
    private void addDevice(String name, int type){
        if(model.size()>=maxdevices){
            btn_Add.setEnabled(false);
            lbl_info.setText("Max devices reached!");
            return;
        }

        model.addElement(new Object[]{name, type});
        if(model.getSize()>0)
            spn_NetDevices.setVisible(true);
        this.updateUI();
        txt_Name.setText("");
    }
    private void Load(){
        ArrayList<Object[]> data = (ArrayList<Object[]>) toolbar.data.get(Namespace.keyMaps.NETWORK);
        if(data==null)return;
        for(int i=0; i<data.size(); i++)
            addDevice((String)data.get(i)[0],(Integer)data.get(i)[1]);
        if(data.size()>0)
            spn_NetDevices.setVisible(true);
    }
    private void save(){
        ArrayList<Object[]>  data = new ArrayList<Object[]>();
        for(int i=0; i<model.getSize(); i++)
            data.add((Object[]) model.get(i));
        toolbar.data.put(Namespace.keyMaps.NETWORK, data);
        toolbar.next();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Add;
    private javax.swing.JButton btn_Cancel;
    private javax.swing.JButton btn_Next;
    private javax.swing.JButton btn_Previous;
    private javax.swing.JComboBox cbb_Type;
    private javax.swing.JLabel lbl_Name;
    private javax.swing.JLabel lbl_info;
    private javax.swing.JLabel lbl_type;
    private javax.swing.JList lst_NetDevices;
    private javax.swing.JPanel pn_Container;
    private javax.swing.JScrollPane spn_NetDevices;
    private javax.swing.JTextField txt_Name;
    // End of variables declaration//GEN-END:variables

}
